package cn.exrick.xboot.modules.transport.controller;

import cn.exrick.xboot.common.lock.Callback;
import cn.exrick.xboot.common.lock.RedisLockTemplate;
import cn.exrick.xboot.common.utils.BeanConverterUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.transport.entity.CompanyWarehouses;
import cn.exrick.xboot.modules.transport.param.DepositAmountParam;
import cn.exrick.xboot.modules.transport.param.WarehousesContactParam;
import cn.exrick.xboot.modules.transport.service.RegionsService;
import cn.exrick.xboot.modules.transport.vo.CompanyWarehousesVo;
import cn.exrick.xboot.modules.transport.param.QueryCompanyWarehousesParam;
import cn.exrick.xboot.modules.transport.param.EditCompanyWarehousesParam;
import cn.exrick.xboot.modules.transport.service.CompanyWarehousesService;
import cn.exrick.xboot.modules.transport.vo.OrdersVo;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

/**
 * @author gj
 */
@Slf4j
@RestController
@Api(tags = "物流公司仓点信息管理接口")
@RequestMapping("/xboot/companyWarehouses")
@Transactional
public class CompanyWarehousesController {

    @Autowired
    private CompanyWarehousesService companyWarehousesService;
    @Autowired
    private RedisLockTemplate redisLockTemplate;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<CompanyWarehousesVo> get(@PathVariable Long id) {
        return new ResultUtil<CompanyWarehousesVo>().setData(companyWarehousesService.getWarehousesById(id));
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<CompanyWarehousesVo> saveOrUpdate(@RequestBody EditCompanyWarehousesParam editCompanyWarehousesParam) {
        //新增时同一公司同一地区下不能有多个仓库
        if (editCompanyWarehousesParam.getId() == null) {
            if (companyWarehousesService.getOne(new QueryWrapper<CompanyWarehouses>().eq("company_id", editCompanyWarehousesParam.getCompanyId()).eq("region_id", editCompanyWarehousesParam.getRegionId())) != null) {
                return new ResultUtil<CompanyWarehousesVo>().setErrorMsg("同一公司同一地区下不能有多个仓库");
            }
        }
        CompanyWarehouses companyWarehouses = BeanConverterUtil.convertBean(editCompanyWarehousesParam, CompanyWarehouses.class);
        if (companyWarehousesService.saveOrUpdate(companyWarehouses)) {
            return new ResultUtil<CompanyWarehousesVo>().setData(BeanConverterUtil.convertBean(editCompanyWarehousesParam, CompanyWarehousesVo.class));
        }
        return new ResultUtil<CompanyWarehousesVo>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/setContact", method = RequestMethod.POST)
    @ApiOperation(value = "设置联系人")
    public Result<CompanyWarehousesVo> setContact(@RequestBody WarehousesContactParam param) {

        if (companyWarehousesService.setContact(param)) {
            return new ResultUtil<CompanyWarehousesVo>().setData(companyWarehousesService.getWarehousesById(param.getWarehouseId()));
        }
        return new ResultUtil<CompanyWarehousesVo>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/depositRecharge", method = RequestMethod.POST)
    @ApiOperation(value = "仓点押金充值")
    public Result<CompanyWarehousesVo> depositRecharge(@RequestBody DepositAmountParam param) {
        Object object = redisLockTemplate.execute("仓点押金充值", 3, null, TimeUnit.SECONDS, new Callback() {
            @Override
            public Object onGetLock() {
                return companyWarehousesService.depositRecharge(param);
            }

            @Override
            public Object onTimeout() {
                return null;
            }
        });
        if (ObjectUtil.isNotNull(object)){
            return new ResultUtil<CompanyWarehousesVo>().setData((CompanyWarehousesVo)object);
        }else {
            return new ResultUtil<CompanyWarehousesVo>().setErrorMsg("服务器处理中，请重试。");
        }
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestBody Long[] ids) {

        for (Long id : ids) {
            companyWarehousesService.removearehousesById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<IPage<CompanyWarehousesVo>> getByCondition(QueryCompanyWarehousesParam param,
                                                             PageVo page){
        return new ResultUtil<IPage<CompanyWarehousesVo>>().setData(companyWarehousesService.getWarehousesByCondition(PageUtil.initMpPage(page),param));
    }
}
